﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Reflection;
using System.Globalization;

namespace FormsData
{
    [Serializable]
    public partial class UserDataGridview : DataGridView
    {
        public bool Auto_sn = true;

        public UserDataGridview()
        {
            InitializeComponent();

            dataGridView_init();
        }

        public void UserDataGridview_Add(string Station, string Bar, string state, Color? _Color = null)
        {
            DataGridViewRowCollection rows = base.Rows;
            object[] values = new string[4] {"", Station, Bar, state};
            rows.Add(values);
            base.Rows[base.Rows.Count - 1].DefaultCellStyle.SelectionForeColor = _Color ?? Color.Red;
            base.Rows[base.Rows.Count - 1].DefaultCellStyle.ForeColor = _Color ?? Color.Red;
        }
        public void dataGridView_init()
        {
            //优化dataGridView控件闪耀现象
            Type dgvType = base.GetType();
            PropertyInfo pi = dgvType.GetProperty("DoubleBuffered", BindingFlags.Instance | BindingFlags.NonPublic);
            pi.SetValue(this, true, null);

            //禁止编辑后自动生成一行
            base.Rows.Clear();
            base.AllowUserToAddRows = false;
            base.BackgroundColor = Color.White;
            base.BorderStyle = BorderStyle.None;
            base.ColumnHeadersBorderStyle = DataGridViewHeaderBorderStyle.Single;
            base.EnableHeadersVisualStyles = false;
            base.GridColor = SystemColors.ActiveCaption;
            base.RowHeadersBorderStyle = DataGridViewHeaderBorderStyle.Single;
            base.RowTemplate.Height = 23;
            base.ScrollBars = ScrollBars.Both;
            base.DefaultCellStyle.Font = new Font("微软雅黑", 10.5f, FontStyle.Regular, GraphicsUnit.Point, 134);
            base.RowHeadersWidth = 60;
            DataGridViewCellStyle dataGridViewCellStyle = new DataGridViewCellStyle();
            DataGridViewCellStyle dataGridViewCellStyle2 = new DataGridViewCellStyle();
            dataGridViewCellStyle.BackColor = Color.FromArgb(245, 245, 255);
            dataGridViewCellStyle.Font = new Font("微软雅黑", 10.5f, FontStyle.Regular, GraphicsUnit.Point, 134);
            dataGridViewCellStyle.ForeColor = SystemColors.WindowText;
            dataGridViewCellStyle.SelectionBackColor = SystemColors.Highlight;
            dataGridViewCellStyle.SelectionForeColor = SystemColors.HighlightText;
            dataGridViewCellStyle.WrapMode = DataGridViewTriState.True;
            base.ColumnHeadersDefaultCellStyle = dataGridViewCellStyle;
            dataGridViewCellStyle2.Alignment = DataGridViewContentAlignment.MiddleLeft;
            dataGridViewCellStyle2.BackColor = Color.FromArgb(245, 245, 255);
            dataGridViewCellStyle2.Font = new Font("微软雅黑", 10.5f, FontStyle.Regular, GraphicsUnit.Point, 134);
            dataGridViewCellStyle2.ForeColor = SystemColors.WindowText;
            dataGridViewCellStyle2.SelectionBackColor = SystemColors.Highlight;
            dataGridViewCellStyle2.SelectionForeColor = SystemColors.HighlightText;
            dataGridViewCellStyle2.WrapMode = DataGridViewTriState.True;
            base.RowHeadersDefaultCellStyle = dataGridViewCellStyle2;


            //禁止编辑
            base.ReadOnly = true;
            //不显示空白新增行
            base.AllowUserToAddRows = false;
            //不显示行表头
            base.RowHeadersVisible = false;
            //是否允许多选行
            base.MultiSelect = false;


            //禁止用户改变列宽行高
            base.AllowUserToResizeColumns = false;
            base.AllowUserToOrderColumns = false;
            base.AllowUserToResizeRows = false;
            base.ColumnHeadersHeightSizeMode = DataGridViewColumnHeadersHeightSizeMode.DisableResizing;
            base.RowHeadersWidthSizeMode = DataGridViewRowHeadersWidthSizeMode.DisableResizing;

        }


        protected override void OnRowStateChanged(int rowIndex, DataGridViewRowStateChangedEventArgs e)
        {
            if (Auto_sn)
            {
                for (int i = 0; i < base.Rows.Count; i++)
                {
                    DataGridViewRow r = base.Rows[i];
                    //r.HeaderCell.Value = string.Format("{0}", i + 1); OnRowStateChanged
                    r.Cells[0].Value = string.Format("{0}", i + 1);
                }
            }
            base.Refresh();
            base.OnRowStateChanged(rowIndex,e);
        }


    }
}
